new Vue({
    el: '#app',
    data() {
        return {
            isflageIOX: true,
            
            headerTitle: '消息通知',
            currentMessageType: 0,

            msgOptions: [{
                messageType: 2,
                title: '检修公告'
            },{
                messageType: 1,
                title: '系统消息'
            },{
                messageType: 3,
                title: '版本更新'
            }],

            messageList: [],
            checkMsgData: [],
            multiChoose: false,
            chooseAll: false,

            announcementRead: true,
            sysMsgRead: true,
            versionRead: true,

            announcementExpand: false,
            sysMsgExpand: false,
            versionExpand: false,

            announcementList: [],
            sysMsgList: [],
            versionList: []
        }
    },
    watch: {
        currentMessageType: {
            handler(newValue, oldValue) {
                if(newValue == 0) {
                    this.headerTitle = '消息通知'
                }else if(newValue == 1) {
                    this.headerTitle = '系统消息'
                }else if(newValue == 2) {
                    this.headerTitle = '检修公告'
                }else if(newValue == 3) {
                    this.headerTitle = '版本更新'
                }
            },
            immediate: true
        },
    },
    created() {
        const _this = this
        let jsApiList = []
        $.getScript("../../../js/jweixin-sgccigw-1.0.0.js" /*"js/jweixin-1.0.0.js"*/, function () {
            'use strict';
            wx.ready(function () {
                wx.onHistoryBack(() => {
                    _this.home()
                    return false
                })
            });
            jsApiList = [
                'onHistoryBack',
            ];
            wx.config({
                beta: true, // 调用wx.invoke形式的接口值时，该值必须为true。
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: corpid,
                timestamp: '1626832073',
                nonceStr: nonceStr,
                signature: signature,
                jsApiList: jsApiList
            });
        });
    },
    mounted() {
        this.compatibleInput()

        // this.getUserMessage()
    },
    methods: {
        toggle(index, checkItem) {
            if(this.multiChoose) {
                const exist = this.checkMsgData.some(item => item.id === checkItem.id)
                if(!exist) {
                    //选中,且全部选中
                    if(this.checkMsgData.length + 1 == this.messageList.length) {
                        this.chooseAll = true
                    }
                }else {
                    this.chooseAll = false
                }
                this.$refs.checkboxes[index].toggle()
            }else {
                if(checkItem['expand']) {
                    this.$set(checkItem, 'expand', false)
                }else {
                    // checkItem['expand'] = true
                    this.$set(checkItem, 'expand', true)
                }
            }
        },
        chooseMsgAllClick(event) {
            if(this.chooseAll) {
                this.$refs.checkboxes.forEach(item => {
                    item.toggle(true);
                })
            }else {
                this.$refs.checkboxes.forEach(item => {
                    item.toggle(false);
                })
            }
        },
        handleDeleteMsg() {
            if(null == this.checkMsgData || 0 == this.checkMsgData.length) {
                mui.toast('请先选择数据', {
                    duration: 1000,
                    type: 'div'
                })
                return
            }

            mui.confirm("是否删除数据", "删除数据", ["取消", "确定"], (e) => {
                if (e.index == 1) {
                    let ids = this.checkMsgData.map(item => item.id).join(',')
                    this.deleteUserMessage(ids)
                } else {
                }
            });
        },
        handleMultiChooseShow() {
            if(!this.messageList || this.messageList.length == 0) {
                return
            }
            this.multiChoose = true
            this.messageList.forEach(item => {
                this.$set(item, 'expand', false)
            })
        },
        handleMultiChooseCancel() {
            this.multiChoose = false
            this.chooseAll = false
            this.checkMsgData = []
        },
        showMsgList(type) {
            this.currentMessageType = type
            this.getUserMessage(type)
        },
        formatTime(time) {
            return time.substr(0, 16)
        },
        expandAnnouncement() {
            // if(this.announcementList && 0 < this.announcementList.length) {
                this.announcementExpand = !this.announcementExpand
                if(!this.announcementRead) {
                    this.readMessage(2)
                }
            // }
        },
        expandSysMsg() {
            // if(this.sysMsgList && 0 < this.sysMsgList.length) {
                this.sysMsgExpand = !this.sysMsgExpand
            // }
            if(!this.sysMsgRead) {
                this.readMessage(1)
            }
        },
        expandVersion() {
            this.versionExpand = !this.versionExpand
            if(!this.versionRead) {
                this.readMessage(3)
            }
        },
        //删除用户消息
        deleteUserMessage(ids) {
            $(".curtain").css("display", "block");
            const userId = sessionStorage.getItem("userId");
            let param = {
                "userId": userId,
                "ids": ids,
                "timestamp": new Date().getTime() + "",
            };
            let AuthenticationSession = sessionStorage.getItem("Authentication");
            data = JSON.stringify(param)
            let sign = sm3(data);
            $.ajax({
                url: localUrlLink + iscp_port + "/tjAiAssistant/userMessage/deleteUserMessage",
                type: "post",
                // async: false, //是否同步，false表示同步请求
                dataType: "json",
                data: data,
                headers: {
                    "Content-Type": "application/json;charset=UTF-8",
                    Authentication: AuthenticationSession,
                    sign: sign
                },
                success: (res, txt, xhr) => {
                    if (sm3(res.resultValueStr) == xhr.getResponseHeader("sign")) {
                        let Authentication = xhr.getResponseHeader("Authentication")
                        if (res.code == 200) {
                            if (Authentication) {
                                sessionStorage.setItem("Authentication", Authentication);
                            }
                            this.handleMultiChooseCancel()
                            this.getUserMessage(this.currentMessageType)
                            // if(1 == item.messageType) {
                            //     this.sysMsgList = this.sysMsgList.filter(o => o.id !== item.id)
                            //     if(0 == this.sysMsgList.length) {
                            //         this.sysMsgExpand = false
                            //     }
                            // }else if(2 == item.messageType) {
                            //     this.announcementList = this.announcementList.filter(o => o.id !== item.id)
                            //     if(0 == this.announcementList.length) {
                            //         this.announcementExpand = false
                            //     }
                            // }else if(3 == item.messageType) {
                            //     this.versionList = this.versionList.filter(o => o.id !== item.id)
                            //     if(0 == this.versionList.length) {
                            //         this.versionExpand = false
                            //     }
                            // }
                        } else {
                            mui.toast('请稍后再试', {
                                duration: 1000,
                                type: 'div'
                            })
                        }
                    }
                    $(".curtain").css("display", "none");
                },
                error: (e) => {
                    $(".curtain").css("display", "none");
                    mui.toast('请稍后再试', {
                        duration: 1000,
                        type: 'div'
                    })
                },
            });
        },
        //读取消息:1-系统消息 2-检修公告 3-版本更新
        readMessage(messageType) {
            $(".curtain").css("display", "block");
            const userId = sessionStorage.getItem("userId");
            let param = {
                "userId": userId,
                "messageType": messageType,
                "timestamp": new Date().getTime() + "",
            };
            let AuthenticationSession = sessionStorage.getItem("Authentication");
            data = JSON.stringify(param)
            let sign = sm3(data);
            $.ajax({
                url: localUrlLink + iscp_port + "/tjAiAssistant/userMessage/readMessage",
                type: "post",
                // async: false, //是否同步，false表示同步请求
                dataType: "json",
                data: data,
                headers: {
                    "Content-Type": "application/json;charset=UTF-8",
                    Authentication: AuthenticationSession,
                    sign: sign
                },
                success: (res, txt, xhr) => {
                    if (sm3(res.resultValueStr) == xhr.getResponseHeader("sign")) {
                        let Authentication = xhr.getResponseHeader("Authentication")
                        if (res.code == 200) {
                            if (Authentication) {
                                sessionStorage.setItem("Authentication", Authentication);
                            }
                            if(1 == messageType) {
                                this.sysMsgRead = true
                            }else if(2 == messageType) {
                                this.announcementRead = true
                            }else if(3 == messageType) {
                                this.versionRead = true
                            }
                        } else {
                            mui.toast('请稍后再试', {
                                duration: 1000,
                                type: 'div'
                            })
                        }
                    }
                    $(".curtain").css("display", "none");
                },
                error: (e) => {
                    $(".curtain").css("display", "none");
                    mui.toast('请稍后再试', {
                        duration: 1000,
                        type: 'div'
                    })
                },
            });
        },
        //获取用户消息
        getUserMessage(type) {
            $(".curtain").css("display", "block");
            const userId = sessionStorage.getItem("userId");
            let param = {
                "userId": userId,
                "messageType": type,
                "history": 1,
                "timestamp": new Date().getTime() + "",
            };
            let AuthenticationSession = sessionStorage.getItem("Authentication");
            data = JSON.stringify(param)
            let sign = sm3(data);
            $.ajax({
                url: localUrlLink + iscp_port + "/tjAiAssistant/userMessage/getUserMessage",
                type: "post",
                // async: false, //是否同步，false表示同步请求
                dataType: "json",
                data: data,
                headers: {
                    "Content-Type": "application/json;charset=UTF-8",
                    Authentication: AuthenticationSession,
                    sign: sign
                },
                success: (res, txt, xhr) => {
                    if (sm3(res.resultValueStr) == xhr.getResponseHeader("sign")) {
                        let Authentication = xhr.getResponseHeader("Authentication")
                        if (res.code == 200) {
                            if (Authentication) {
                                sessionStorage.setItem("Authentication", Authentication);
                            }
                            this.messageList = res.resultValue
                            // let data = res.resultValue
                            // this.sysMsgRead = data.sysMsgRead
                            // this.sysMsgList = data.sysMsgList
                            // this.announcementRead = data.announcementRead
                            // this.announcementList = data.announcementList
                            // this.versionRead = data.versionRead
                            // this.versionList = data.versionList
                        } else {
                            mui.toast('请稍后再试', {
                                duration: 1000,
                                type: 'div'
                            })
                        }
                        $(".curtain").css("display", "none");
                    }
                },
                error: (e) => {
                    $(".curtain").css("display", "none");
                    mui.toast('请稍后再试', {
                        duration: 1000,
                        type: 'div'
                    })
                },
            });
        },
        // 判断安卓还是ios
        compatibleInput() {
            //获取浏览器的userAgent,并转化为小写
            var ua = navigator.userAgent.toLowerCase();
            //判断是否是苹果手机，是则是true
            var isIos = ua.indexOf("iphone") != -1 || ua.indexOf("ipad") != -1;
            if (isIos) {
                this.isflageIOX = true;
            } else {
                this.isflageIOX = false;
            }
        },
        home() {
            if(0 != this.currentMessageType) {
                this.handleMultiChooseCancel()
                this.currentMessageType = 0
                return
            }
            $(location).attr("href", "../../Home.html");
        },
        createSgin(data) {
            let hashData = sm3(data);
            let hashData_last = sm3(hashData);
            hashData_last = hashData_last.substr(0, 5);
            hashData += hashData_last;
            let sign = hashData;
            return sign;
        },
    }
})